package Server;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import packages.Packages;

public class ConnectedClient extends Thread {

	private Socket socket;
	private OutputStream ostream;
	private InputStream istream;
	private MailBox mail;
	private Game game;
	private String name;

	public ConnectedClient(Socket socket, MailBox mail) {
		this.socket = socket;
		this.mail = mail;
		try {
			ostream = socket.getOutputStream();
			istream = socket.getInputStream();
			// Getting the name
			istream.read();
			name = readMessage(istream).trim();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.start();
	}

	public void setGame(Game game) {
		this.game = game;
	}

	@Override
	public void run() {
		try {
			while (socket.isConnected() && !interrupted()) {
				int comm = istream.read();
				String message = "";
				switch (comm) {
				case Packages.PACKAGE_CHAT_MESSAGE:// PACKAGE_CHAT_MESSAGE
					String temp = readMessage(istream);
					if (temp.contains("START") && !game.isStarted()) {
						game.setupNewGame(null);
					} else if (game.isDrawer(this) && temp.contains("CLEAR")) {
						game.sendToAll("", Packages.PACKAGE_CLEAR);
					} else if (game.isDrawer(this)
							&& temp.contains("WORD_ACCEPTED")) {
						game.newGame(true);
					} else if (game.isDrawer(this)
							&& temp.contains("WORD_DENIED")) {
						game.newGame(false);
					} else if (game.checkGuess(temp)) {
						game.endGame(name);
						game.setupNewGame(this);
					} else {
						mail.putMessage(name + ": " + temp);
					}
					break;
				case Packages.PACKAGE_POSITIONS:// PACKAGE_POSITIONS
					message = readMessage(istream);
					game.sendToAllButPainter(message,
							Packages.PACKAGE_POSITIONS);
					break;
				case Packages.PACKAGE_ROUND_OVER: // PACKAGE_ROUND_OVER
					break;

				case Packages.PACKAGE_GET_CLIENT_LIST: // PACKAGE_GET_CLIENT_LIST
					break;
				case Packages.PACKAGE_PLAYER_NAME:
					name = readMessage(istream);
					break;
				default:
					if (comm == -1) {
						boolean isCurrentDrawer = game.isDrawer(this);
						game.disconnectClient(this);
						game.updateClientList();
						if (isCurrentDrawer) {
							// game.newGame(null);
							game.setupNewGame(null);
						}
						socket.close();
						interrupt();
					}
					break;
				}

			}

		} catch (IOException e) {
			game.updateClientList();
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}
	}

	private String readMessage(InputStream is) throws IOException {
		byte[] buffer = new byte[2048];
		int offset = 0;
		is.read(buffer, offset, 1);
		while (buffer[offset] != '\n') {
			offset++;
			is.read(buffer, offset, 1);
		}
		return new String(buffer, 0, offset + 1, "UTF-8");

	}

	public void send(String message, byte packageType) {
		try {
			ostream.write(packageType);
			ostream.write((message).getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String getPlayerName() {
		return name;
	}

	public boolean isConnected() {

		return !socket.isClosed();
	}

}
